package com.smartlink.dao;

import java.math.BigInteger;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.smartlink.pojo.Classroom;
import com.smartlink.pojo.OnLineStudent;

public class SignInDao {
	private SessionFactory sessionFactory;
	private Session session;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Session getSession() {
		this.session = sessionFactory.getCurrentSession();
		return session;
	}

	/**
	 * 根据学号查询学生是否在线
	 * 
	 * @param studentId
	 * @return
	 */
	public OnLineStudent getOnLineStudentByStudentId(String studentId) {
		String hqlString = "FROM OnLineStudent WHERE student_id = ?";
		OnLineStudent onLineStudent = (OnLineStudent) getSession().createQuery(hqlString).setString(0, studentId)
				.uniqueResult();
		return onLineStudent;
	}

	/**
	 * 根据楼id获取教室
	 * 
	 * @param buildingId
	 * @return
	 */
	public List<Classroom> getClassroomByBuildingId(int buildingId) {
		String hql = "FROM Classroom WHERE building_id = ?";
		@SuppressWarnings("unchecked")
		List<Classroom> classrooms = getSession().createQuery(hql).setInteger(0, buildingId).list();
		return classrooms;
	}

	/**
	 * 将学生加入在线列表中
	 * 
	 * @param number
	 */
	public void addStudent(String number) {
		int clazzId = getClazzIdByStudentNo(number);
		OnLineStudent onLineStudent = new OnLineStudent();
		onLineStudent.setStudentId(number);
		onLineStudent.setClazzId(clazzId);
		getSession().save(onLineStudent);

	}

	/**
	 * 根据学号获取所属班级Id
	 * 
	 * @return
	 */
	public int getClazzIdByStudentNo(String number) {
		String sql = "SELECT s.`clazz_id` FROM student s WHERE s.`student_id`=?";
		Integer clazzId = (Integer) getSession().createSQLQuery(sql).setString(0, number).uniqueResult();
		return clazzId;
	}

	/**
	 * 根据教室id查询教室总人数
	 * 
	 * @param classroomId
	 * @return
	 */
	public int getClazzTotalStudent(int classroomId) {
		String sql = "SELECT COUNT(s.`phone`) total FROM student s WHERE s.`CLAZZ_ID`=?;";
		SQLQuery query = getSession().createSQLQuery(sql);
		BigInteger total = (BigInteger) query.setInteger(0, classroomId).uniqueResult();
		return total.intValue();
	}

	/**
	 * 根据班级获取没有签到的学生信息
	 * 
	 * @param classroomId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Object[]> getNoSiginStudent(int classroomId) {
		String sql = "SELECT s.`name`,s.`student_id`FROM student s WHERE s.`student_id`NOT IN (SELECT ols.`student_id` FROM on_line_student ols WHERE ols.clazz_id=?) AND s.`CLAZZ_ID`=?";
		SQLQuery query = getSession().createSQLQuery(sql);
		List<Object[]> students = query.setInteger(0, classroomId).setInteger(1, classroomId).list();
		return students;
	}

	/**
	 * 修改学生脸部识别标记
	 * 
	 * @param studentId
	 * @return
	 */
	public boolean setStudentFace(String studentId) {
		String sql = "UPDATE student s SET s.face_flag=1 WHERE s.`student_id`=?";
		int num = getSession().createSQLQuery(sql).setString(0, studentId).executeUpdate();
		if (num > 0) {
			return true;
		}
		return false;
	}

}
